package ru.spb.osll.myfit.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import ru.spb.osll.myfit.client.dialog.ErrorDialog;
import ru.spb.osll.myfit.client.service.SuiteService;
import ru.spb.osll.myfit.client.service.SuiteServiceAsync;
import ru.spb.osll.myfit.common.Suite;

import java.util.List;

/**
 *
 */
public class SuiteTestsWidget extends Composite {
  private final VerticalPanel m_widget = new VerticalPanel();
  private final SuiteServiceAsync m_suiteService = GWT.create(SuiteService.class);
  private final ErrorDialog m_errorDialog = new ErrorDialog();

  public SuiteTestsWidget() {
    m_widget.addStyleName(IStyleConstants.SUITE_TESTS_PANEL_STYLE);
    m_widget.add(new Label("Pages: "));
    initWidget(m_widget);
    setVisible(false);
  }

  public void update(Suite suite) {
    m_suiteService.getSuitePages(suite, new AsyncCallback<List<String>>() {
      @Override
      public void onFailure(Throwable throwable) {
        m_errorDialog.setMessage(throwable);
        m_errorDialog.center();
      }

      @Override
      public void onSuccess(List<String> paths) {
        m_widget.clear();
        for (String path : paths) {
          m_widget.add(new Anchor(path, path));
        }
        m_widget.setVisible(paths.size() > 0);
      }
    });
  }
}
